package com.ita.os.client.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ita.os.client.po.ClientUser;
import com.ita.os.client.service.ClientUserService;
import com.ita.os.client.service.impl.ClientUserServiceImpl;
import com.ita.os.client.util.StringUtil;

/**
 * Servlet implementation class ClientUserServlet
 */
@WebServlet("/clientuser")
public class ClientUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ClientUserService userService = new ClientUserServiceImpl();
       
    public ClientUserServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action=request.getParameter("action");
		if("login".equals(action)){
			login(request,response);
		}else if("register".equals(action)){
			register(request,response);
		}else if("logout".equals(action)){
			logout(request,response);
		}
	}

	private void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String tel = request.getParameter("tel");
		String name = request.getParameter("uname");
        String pwd = request.getParameter("password");
		if(StringUtil.isEmpty(tel) || StringUtil.isEmpty(name) || StringUtil.isEmpty(pwd)){
			String msg = "请完善注册信息！！！";
            request.setAttribute("msg",msg);
            request.getRequestDispatcher("/register.jsp").forward(request, response);
            return;
		}
		ClientUser user = new ClientUser();
		user.setUname(name);
		user.setTel(tel);
		user.setPwd(pwd);
		userService.addUser(user);
		String message = String.format(
                "注册成功！3秒后为您自动跳到首页！<meta http-equiv='refresh' content='3;url=%s'/>", 
                request.getContextPath()+"/index");
		request.getSession().setAttribute("user", user);
        request.setAttribute("message",message);
        request.getRequestDispatcher("/message.jsp").forward(request,response);
	}

	private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String tel = request.getParameter("tel");
        String pwd = request.getParameter("password");
		ClientUser user = userService.findUser(tel, pwd);
		if(user == null){
			String msg = "对不起，用户名或密码有误！！请重新输入！";
            request.setAttribute("msg",msg);
            request.getRequestDispatcher("/login.jsp").forward(request, response);
            return;
        }
        request.getSession().setAttribute("user", user);
        response.sendRedirect("index");
	}
	
	private void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
		HttpSession session = request.getSession();
		session.invalidate();
		response.sendRedirect("index");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
